
#define CleanSelf_Variable
#include "userDef.h"



CleanSelfTyoe CleanSelfData;

/* called in systime.c per 1s */

/*
void cleanSelfAct( void )
{
    static U8 timer_CSelf_Cool_4min = 0;
    static U8 timer_CSelf_Cool_3min = 0;
    static U8 timer_CSelf_Fan_3min = 0;
    static U8 timer_CSelf_Heat_3min = 0;
    static U8 timer_CSelf_Heat_1min = 0;
    static U16 timer_CSelf_Cool_10min = 0;

    if ( f_CleanSelf == 1 )
    {
        if ( f_OutCommErr || f_CommErr || ErrFlagQT > 0 || ErrFlag > 0 || OutSafeErr1 > 0 || OutSafeErr2 > 0 || OutSafeErr3 > 0 || OutSafeErr4 > 0 || OutSafeErr5 > 0 || OutSafeErr6 > 0 || OutSafeErr7 > 0 || OutSafeErr8 > 0 )
        {
            stat_CleanSelf = CSELF_END;
        }

        switch ( stat_CleanSelf ) // 自清洁 标志
        {
            case CSELF_ACT:
                stat_CleanSelf = CSELF_COOL;
                CleanSelfMode();
                break;

            case CSELF_COOL:
                if ( temp.tube > ( 400 + 0 ) ) // 若T内管 > 0℃下运行时间超过10min,则退出自动清洁
                {
                    if ( timer_CSelf_Cool_10min < 600 ) // 制冷10分钟 ，结束
                    {
                        timer_CSelf_Cool_10min++;
                    }
                    else 
                    {
                        stat_CleanSelf = CSELF_END;
                        CleanSelfMode();
                    }
                }
                else if ( temp.tube > ( 400 - 100 ) ) // 管温 大于-10 ，小于0 ，
                {
                    if ( timer_CSelf_Cool_4min < 240 ) // 运行4分钟 
                    {
                        timer_CSelf_Cool_4min++;
                    }
                    else 
                    {
                        stat_CleanSelf = CSELF_FAN;
                        CleanSelfMode();
                    }
                }
                else // 管温小于10 度
                {
                    if ( timer_CSelf_Cool_3min < 180 ) // 运行3分钟
                    {
                        timer_CSelf_Cool_3min++;
                    }
                    else 
                    {
                        stat_CleanSelf = CSELF_FAN;
                        CleanSelfMode();
                    }
                }

                break;

            case CSELF_FAN: // 风扇自清洁
                if ( timer_CSelf_Fan_3min < 180 ) // 运行3分钟
                {
                    timer_CSelf_Fan_3min++;
                }
                else 
                {
                    stat_CleanSelf = CSELF_HEAT_L; // 低风速制热
                    CleanSelfMode();
                }

                break;

            case CSELF_HEAT_L:
                if ( timer_CSelf_Heat_3min < 180 ) // 低风速制热3分钟
                {
                    timer_CSelf_Heat_3min++;
                }
                else 
                {
                    stat_CleanSelf = CSELF_HEAT_H; // 高温制热
                    CleanSelfMode();
                }

                break;

            case CSELF_HEAT_H: // 高温制热运行1分钟，结束
                if ( timer_CSelf_Heat_1min < 60 )
                {
                    timer_CSelf_Heat_1min++;
                }
                else 
                {
                    stat_CleanSelf = CSELF_END;
                    CleanSelfMode();
                }

                break;

            default:
                CleanSelfMode();
                break;
        }
    }
    else 
    {
        stat_CleanSelf = CSELF_END;
        timer_CSelf_Cool_10min = 0;
        timer_CSelf_Cool_4min = 0;
        timer_CSelf_Cool_3min = 0;
        timer_CSelf_Fan_3min = 0;
        timer_CSelf_Heat_3min = 0;
        timer_CSelf_Heat_1min = 0;
    }
}

*/
/*
void CleanSelfMode( void )
{
    switch ( stat_CleanSelf )
    {
        case CSELF_COOL:
            set_power( ON );
            set_mode( MODECOOL );
            set_fanStep( FANLEVEL2 );
            set_tartemp( 160, 610 );
            break;

        case CSELF_FAN:
            set_power( ON );
            set_mode( MODEFAN );
            set_fanStep( FANLEVEL2 );
            set_tartemp( 250, 770 );
            break;

        case CSELF_HEAT_L:
            set_power( ON );
            set_mode( MODEHEAT );
            set_fanStep( FANLEVEL2 );
            set_tartemp( 310, 880 );
            break;

        case CSELF_HEAT_H:
            set_power( ON );
            set_mode( MODEHEAT );
            set_fanStep( FANLEVEL6 );
            set_tartemp( 310, 880 );
            break;

        default:
            f_CleanSelf = 0;
            set_power( OFF );
            set_mode( MODEAUTO );
            set_tartemp( 250, 770 );
            set_fanStep( FANAUTO );
            break;
    }

    set_verSw( 0x02 );
    set_horSw( 0x00 );
    set_light( ON );
    set_sleep( OFF );
    set_health( OFF );
    set_dryer( OFF );
    set_heater( OFF );
    set_eco( OFF );
    set_lowheat( OFF );
    temp.IFeel = 0;
    set_timerOn( 0 );
    set_timerOff( 0 );

#ifdef CTRL_485
    update_wireCtrl = 2;
#endif
}
*/











// 滞环算法函数
U8 CleanSelfInnerCoilTemperatureFanCtrl(U16 input, U8 previous_state) {
    if (previous_state == FANLEVEL6) {
        // 从高到低的切换逻辑
        if (input < HIGH_TO_LOW_3) return FANSTOP;  //状态3
        if (input < HIGH_TO_LOW_2) return FANLEVEL2;//状态2
        if (input < HIGH_TO_LOW_1) return FANLEVEL4;//状态1
        return previous_state; // 保持当前状态
    } else if (previous_state == FANLEVEL4) {
        // 从低到高的切换逻辑
        if (input > LOW_TO_HIGH_3) return FANLEVEL6;//状态0
        if (input < HIGH_TO_LOW_3) return FANSTOP;  //状态3
        if (input < HIGH_TO_LOW_2) return FANLEVEL2;//状态2
        return previous_state; // 保持当前状态
    } else if (previous_state == FANLEVEL2) {
        // 从低到高的切换逻辑
        if (input > LOW_TO_HIGH_3) return FANLEVEL6;//状态0
        if (input > LOW_TO_HIGH_2) return FANLEVEL4;//状态1
        if (input < HIGH_TO_LOW_3) return FANSTOP;  //状态3
        return previous_state; // 保持当前状态
    } else if (previous_state == FANSTOP) {
        // 从低到高的切换逻辑
        if (input > LOW_TO_HIGH_3) return FANLEVEL6;//状态0
        if (input > LOW_TO_HIGH_2) return FANLEVEL4;//状态1
        if (input > LOW_TO_HIGH_1) return FANLEVEL2;//状态2		
        return previous_state; // 保持当前状态
    }
    return previous_state; // 保持当前状态
}









void CleanSelfAct( void )
{
    static U8 Flag1 = 0;
    static U8 BuzSingCount = 0;         // 蜂鸣器鸣叫次数

    // 开机切换到 关机
    {
        if ( f_CleanSelf == ON )
        {
            if ( b_power.set == OFF )
            {
                if ( Flag1 == 1 ) // 由开机 到关机 
                {
                    CleanSelfData.OffCleanFlag = PowerOnOffGenerateCleanOff;
                    Flag1   = 0;
                }
            }
        }

        if ( b_power.set == ON )
        {
            Flag1   = 1;
        }
    }

    // 正在自清洁，收到关机信号时候，关掉自清洁
    {
        if ( CleanSelfData.OutCleanStartFlag == CleanOn )
        {
            if ( b_power.set == OFF )
            {
                CleanSelfData.OffCleanFlag = PowerOnOffGenerateCleanOff;
            }
        }
    }

    // 触发错误报警
    {
        if ( CleanSelfData.OutCleanStartFlag == CleanOn )
        {
            if ( f_OutCommErr || f_CommErr || ErrFlagQT > 0 || ErrFlag > 0 || OutSafeErr1 > 0 || OutSafeErr2 > 0 || OutSafeErr3 > 0 || OutSafeErr4 > 0 || OutSafeErr5 > 0 || OutSafeErr6 > 0 || ( ( OutSafeErr7 > 0 ) && ( OutSafeErr7 != 0x02 ) ) ||
                 OutSafeErr8 > 0 ) //防冻结除外
            {
                CleanSelfData.OffCleanFlag = ErrGenerateCleanOff;
            }
        }
    }

    // 执行自清洁完成。
    {
        if ( CleanSelfData.OutCleanStartFlag == 0 )
        {
            if ( CleanSelfData.RunTime > ( ( ( 6 + 10 ) * 60 ) + 22 + 10 + 30 ) ) // 具体逻辑 参考规格书
            {
                CleanSelfData.OffCleanFlag = CleanFinishGenerateCleanOff;
            }
        }
    }

    // 自清洁计时
    {
        if ( CleanSelfData.CleanStartFlag == 1 )
        {
            CleanSelfData.RunTime++;
        }
    }

    // 内机触发自清洁标志且外机开始自清洁 自清洁正式启动
    {
        if ( ( f_CleanSelf == ON ) && ( CleanSelfData.OutCleanStartFlag == CleanOn ) )
        {
            CleanSelfData.CleanStartFlag = CleanOn;
        }
    }

    // 自清洁风机运行逻辑
    {
        if ( CleanSelfData.OutCleanStartFlag == CleanOn )
        {
            if ( ( CleanSelfData.RunTime >= 0 ) && ( CleanSelfData.RunTime <= 6 * 60 ) ) // 微风
            {
                e_fanStep.run = FANLEVEL2;
            }
            else if ( ( ( CleanSelfData.RunTime >= 6 * 60 ) && ( CleanSelfData.RunTime <= ( ( ( 6 + 10 ) * 60 ) + 22 + 10 + 30 ) ) ) ) // 关风机
            {
                e_fanStep.run = FANSTOP;
            }
            else if ( CleanSelfData.RunTime > ( ( ( 6 + 10 ) * 60 ) + 22 + 10 + 30 ) ) // 内盘管温控制
            {
                e_fanStep.run=CleanSelfInnerCoilTemperatureFanCtrl(temp.tubeAct,e_fanStep.run);
            }
        }
    }

    // 关闭自清洁，并清0
    {
        if ( CleanSelfData.OffCleanFlag != 0 )
        {
            CleanSelfData.OffCleanFlag = 0;
            set_power( OFF );
            CleanSelf();
            BuzSingCount = 2;
        }
    }

    // 蜂鸣器鸣叫
    {
        if ( BuzSingCount > 0 )
        {
            BuzzerAct( BUZZER400ms );   // 短鸣
            BuzSingCount--;
        }
    }
}


void CleanSelfMode( void )
{
    set_power( OFF );
    f_CleanSelf = 0;
    CleanSelfData.RunTime = 0;
    CleanSelfData.OutCleanStartFlag = 0; // 外机开始运行自清洁标志
    CleanSelfData.CleanStartFlag = CleanOff; // 外机开始运行自清洁标志
}


void CleanSelf( void )
{
    f_CleanSelf = 0;
    CleanSelfData.RunTime = 0;
    CleanSelfData.OutCleanStartFlag = 0; // 外机开始运行自清洁标志
    CleanSelfData.CleanStartFlag = CleanOff; // 外机开始运行自清洁标志
}


